package com.ruoyi.other.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.check.CheckEmptyUtil;
import com.ruoyi.other.domain.CityConfig;
import com.ruoyi.other.domain.CityFareConfig;
import com.ruoyi.other.mapper.CityConfigMapper;
import com.ruoyi.other.mapper.CityFareConfigMapper;
import com.ruoyi.other.service.ICityFareConfigService;
import com.ruoyi.other.vo.AddCityFareCityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 城市路费配置Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-16
 */
@Service
public class CityFareConfigServiceImpl implements ICityFareConfigService {
    @Autowired
    private CityFareConfigMapper cityFareConfigMapper;
    @Resource
    private CityConfigMapper cityConfigMapper;

    /**
     * 查询城市路费配置
     *
     * @param id 城市路费配置主键
     * @return 城市路费配置
     */
    @Override
    public CityFareConfig selectCityFareConfigById(Long id) {
        return cityFareConfigMapper.selectCityFareConfigById(id);
    }

    /**
     * 查询城市路费配置列表
     *
     * @param cityFareConfig 城市路费配置
     * @return 城市路费配置
     */
    @Override
    public List<CityFareConfig> selectCityFareConfigList(CityFareConfig cityFareConfig) {
        return cityFareConfigMapper.selectCityFareConfigList(cityFareConfig);
    }

    /**
     * 新增城市路费配置
     *
     * @param cityFareConfig 城市路费配置
     * @return 结果
     */
    @Override
    public int insertCityFareConfig(CityFareConfig cityFareConfig) {
        CityFareConfig cityFareConfig1 = cityFareConfigMapper.selectOne(cityFareConfig.getPartnerId(), cityFareConfig.getCityId());
        if (!CheckEmptyUtil.isEmpty(cityFareConfig1)) {
            throw new RuntimeException("该城市已设置");
        }

        Long partnerId = cityFareConfigMapper.selectPartnerCityFareConfigById(cityFareConfig.getCityId());
        cityFareConfig.setPartnerId(partnerId);

        cityFareConfig.setCreateTime(DateUtils.getNowDate());
        return cityFareConfigMapper.insertCityFareConfig(cityFareConfig);
    }

    /**
     * 修改城市路费配置
     *
     * @param cityFareConfig 城市路费配置
     * @return 结果
     */
    @Override
    public int updateCityFareConfig(CityFareConfig cityFareConfig) {
        cityFareConfig.setUpdateTime(DateUtils.getNowDate());
        return cityFareConfigMapper.updateCityFareConfig(cityFareConfig);
    }

    /**
     * 批量删除城市路费配置
     *
     * @param ids 需要删除的城市路费配置主键
     * @return 结果
     */
    @Override
    public int deleteCityFareConfigByIds(Long[] ids) {
        return cityFareConfigMapper.deleteCityFareConfigByIds(ids);
    }

    /**
     * 删除城市路费配置信息
     *
     * @param id 城市路费配置主键
     * @return 结果
     */
    @Override
    public int deleteCityFareConfigById(Long id) {
        return cityFareConfigMapper.deleteCityFareConfigById(id);
    }

    @Override
    public List<AddCityFareCityVo> selectCityFareCity() {
        List<CityConfig> cityConfigs = cityConfigMapper.selectAllCity();
        Map<Long, AddCityFareCityVo> cityMap = new HashMap<>();
        List<AddCityFareCityVo> rootCities = new ArrayList<>();
        if (CheckEmptyUtil.isEmpty(cityConfigs)) {
            return Collections.emptyList();
        }
        // 第一步：将所有城市转换为AddCityFareCityVo对象，并放入Map中
        for (CityConfig cityConfig : cityConfigs) {
            AddCityFareCityVo cityVo = new AddCityFareCityVo();
            cityVo.setId(cityConfig.getId());
            cityVo.setCityName(cityConfig.getCityName());
            cityVo.setChildren(new ArrayList<>());
            cityMap.put(cityConfig.getId(), cityVo);
        }

        // 第二步：构建城市树结构
        for (CityConfig cityConfig : cityConfigs) {
            AddCityFareCityVo cityVo = cityMap.get(cityConfig.getId());
            if (cityConfig.getPartnerId() == 0) {
                // 如果是顶级城市，则添加到根城市列表中
                rootCities.add(cityVo);
            } else {
                // 否则，添加到对应父城市的children列表中
                AddCityFareCityVo parentCityVo = cityMap.get(cityConfig.getPartnerId());
                if (parentCityVo != null) {
                    parentCityVo.getChildren().add(cityVo);
                }
            }
        }

        return rootCities;
    }
}
